﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JQuery
{
    public class ResourceManager
        : Dictionary<string, Resource.Content>
    {
        public List<Resource.Content> ResourceList(string type)
        {
            List<Resource.Content> oItems = new List<Resource.Content>();

            foreach (string key in Keys)
            {
                if (this[key].type.Eq(type))
                    oItems.Add(this[key]);
            }

            return oItems.OrderBy(x => x.ordinal).ToList();
        }

        public new Resource.Content this[string key]
        {
            get
            {
                return ContainsKey(key) ? base[key] : null;
            }
        }

        public void Add(Resource.Content resource)
        {
            string key = resource.owner.GetResourceKey(resource.id, resource.type).Trim('/','\\',' ');

            if (!ContainsKey(key))
                Add(key, resource);
        }

        public void Clear(string type)
        {
            foreach(Resource.Content item in ResourceList(type))
            {
                Remove(item.key);
            }
        }
    }

    public class PageManager
        : Dictionary<string, ResourceManager>
    {
        public new ResourceManager this[string key]
        {
            get
            {
                if (ContainsKey(key))
                    return base[key];
                else
                    return null;
            }
            set
            {
                if (ContainsKey(key))
                    base[key] = value;
                else
                    Add(key, value);
            }
        }
    }
}
